<template>
  <div>
  </div>
</template>

<script>
import SysExportLogApiMain from '@/api/sys.exportlog';
export default {
  props: {
  },
  data() {
    return {
      fullLoading: true,
      tExportLog: null,  
    }
  },
  computed: {
  },
  mounted () {
  },
  methods: {
    // 导出
    async handleExportExcel (ApiMain, mainQuery, unCheckTime = false, checkTimeField = 'createTimeRange') {
      console.log(mainQuery);
      // 限定时间
      if ((mainQuery[checkTimeField] == undefined || mainQuery[checkTimeField].length == 0) && !unCheckTime) {
        this.$message({message: '请选择时间区间', type: 'warning'})
        return
      }
      mainQuery.unCheckTime = !!unCheckTime

      var re = await ApiMain.exportExcel({ page: 1, search: mainQuery });
      this.fullLoading = this.$loading({
        lock: true,
        text: '导出文件正在生成...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 定时器3s刷一次
      let _this = this;
      this.tExportLog = setInterval(() => {
        _this.checkSysExportLog(re);
      }, 3000)
    },

    async checkSysExportLog (id) {
      var re = await SysExportLogApiMain.info({ id: id });
      if (re.status == 2) {
        clearInterval(this.tExportLog)
        this.fullLoading.close()
        this.$message({message: re.remark, type: 'error'});
      }
      if (re.status == 1) {
        this.fullLoading.close()
        this.$alert('文件已生成，浏览器自动下载；<br/>PS:如果有提示弹出层被屏蔽，请允许浏览器下载', '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '确定',
          callback: action => {}
        })
        clearInterval(this.tExportLog)
        this.doExportFile(id, re.fileName);
      }
    },

    async doExportFile(id, fileName) {
      await SysExportLogApiMain.export({ id: id, fileName: fileName });
    }
  }
}
</script>